#include <SFML/Graphics/VertexArray.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <nice2d/charts/grid.h>

class GridData
{
public:
    sf::VertexArray vertices;
    sf::Color color;
    sf::Vector2f size;
};

Grid::Grid():
    data(new GridData)
{
}

Grid::~Grid()
{
}

void Grid::initial(const sf::Vector2f &size, const sf::Color &color, uint32_t count)
{
    data->color = color;
    data->size = size;
    data->vertices.clear();

    data->vertices.setPrimitiveType(sf::Lines);
    float offset = data->size.y / count;
    for (uint32_t i = 1; i < count; i++) {
        data->vertices.append(sf::Vertex(sf::Vector2f(0, i * offset), data->color));
        data->vertices.append(sf::Vertex(sf::Vector2f(data->size.x, i * offset), data->color));
    }

    offset = data->size.x / count;
    for (uint32_t i = 1; i < count; i++) {
        data->vertices.append(sf::Vertex(sf::Vector2f(i * offset, 0), data->color));
        data->vertices.append(sf::Vertex(sf::Vector2f(i * offset, data->size.y), data->color));
    }
}

void Grid::onDrawNode(sf::RenderTarget &target, sf::RenderStates states) const
{
    target.draw(data->vertices, states);
}
